package com.example.basic;

/**
 * @author zhangs
 * @version v1.0
 * @decription 线程常见的基础属性
 * @date 2021/3/28 11:39
 **/
public class ThreadProperty {
    static void printProperties() {
        /* 获取线程属性 */
        Thread cur = Thread.currentThread();
        String name = cur.getName();
        // 线程id
        System.out.println(name + " ID:" + cur.getId());
        // 线程名称name
        System.out.println(name + " name:" + cur.getName());
        // 线程级别，守护线程和非守护线程
        System.out.println(name + " daemon:" + cur.isDaemon());
        // 线程优先级
        System.out.println(name + " priority:" + cur.getPriority());
    }

    public static void main(String[] args) throws InterruptedException {
        Thread.currentThread().setName("demo main");
        Thread.currentThread().setPriority(10);
        printProperties();

        /* 线程属性设置 */
        Runnable run = ThreadProperty::printProperties;

        Thread thread = new Thread(run);
        // 设置线程运行相关属性
        thread.setDaemon(true); // 守护线程
        thread.setName("run");  // 线程名字
        thread.setPriority(10); // 线程优先级

        // 线程属性daemon必须在运行之前设定
        thread.start();
        // main线程等待thread线程执行完毕
        thread.join();
    }
}
